package org.hypnus.e.tag;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.BodyTag;
import javax.servlet.jsp.tagext.BodyTagSupport;

import org.hypnus.e.sub.EDataset;
import org.hypnus.e.sub.EGrid;
import org.hypnus.e.util.SymbolConstants;
import org.hypnus.e.util.TagType;
import org.hypnus.e.util.TagUtil;

public class DataGridTag extends BodyTagSupport {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	private String id ;
	
	private String title ;
	
	private String dataoptions ;
	
	private String styleclass ;
	
	private String action ;
	
	private String method ;
	
	private String dataset ;
	
	private String style ;
	
	public String getStyle() {
		return style;
	}

	public void setStyle(String style) {
		this.style = style;
	}

	protected String type = TagType.GRID ;
	
	public String getType() {
		return type;
	}

	public void setType(String type) {
		this.type = type;
	}

	public String getDataset() {
		return dataset;
	}

	public void setDataset(String dataset) {
		this.dataset = dataset;
	}

	public String getAction() {
		return action;
	}

	public void setAction(String action) {
		this.action = action;
	}

	public String getMethod() {
		return method;
	}

	public void setMethod(String method) {
		this.method = method;
	}

	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public String getDataoptions() {
		return dataoptions;
	}

	public void setDataoptions(String dataoptions) {
		this.dataoptions = dataoptions;
	}

	public String getStyleclass() {
		return styleclass;
	}

	public void setStyleclass(String styleclass) {
		this.styleclass = styleclass;
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	@Override
	public int doStartTag() throws JspException {
		TagUtil tagUtil = TagUtil.getInstance() ;
		
		EDataset dataset = tagUtil.getEContext(pageContext).getDst(this.getDataset()) ;
		EGrid eGrid = dataset.getGrid(this.getId());
		eGrid.setId(this.getId()) ;
		eGrid.setDataset(this.getDataset()) ;
//		dataset.addGrid(eGrid);
		
		StringBuilder builder = tagUtil.prepareAttribute(this, TagType.TABLE, false) ;
		builder.append(SymbolConstants.LEFT_ANGLE).append(TagType.THEAD).append(SymbolConstants.RIGHT_ANGLE);
		builder.append(SymbolConstants.LEFT_ANGLE).append(TagType.TR).append(SymbolConstants.RIGHT_ANGLE);
		TagUtil.getInstance().write(pageContext, builder.toString()) ;
		return BodyTag.EVAL_BODY_INCLUDE;
	}

	@Override
	public int doEndTag() throws JspException {
		TagUtil tagUtil = TagUtil.getInstance() ;
		StringBuilder builder = new StringBuilder(32) ;
		builder.append(SymbolConstants.LEFT_ANGLE).append(SymbolConstants.SLASH).append(TagType.TR).append(SymbolConstants.RIGHT_ANGLE);
		builder.append(SymbolConstants.LEFT_ANGLE).append(SymbolConstants.SLASH).append(TagType.THEAD).append(SymbolConstants.RIGHT_ANGLE);
		builder.append(SymbolConstants.LEFT_ANGLE).append(SymbolConstants.SLASH).append(TagType.TABLE).append(SymbolConstants.RIGHT_ANGLE);
		tagUtil.write(pageContext, builder.toString()) ;
		
		
//		EDataset dataset = tagUtil.getEContext(pageContext).getDst(this.getDataset()) ;
//		EGrid eGrid = new  EGrid() ;
//		eGrid.setId(this.getId()) ;
//		eGrid.setDataset(this.getDataset()) ;
//		dataset.addGrid(eGrid);
		return BodyTag.EVAL_PAGE;
	}

}
